package Practice17;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Ticket2 implements Runnable {
    private int ticket = 100;
    Lock lock = new ReentrantLock();

    // 执行买票操作
    @Override
    public void run() {
        while (true) {
            lock.lock();
            if (ticket > 0) {
                // 有票
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.fillInStackTrace();
                }
                String name = Thread.currentThread().getName();
                System.out.println(name + "正在卖：" + ticket--);
            }
            lock.unlock();
        }
    }
}